//
//  PGWebViewController.m
//  youqu
//
//  Created by 彭鹏 on 16/10/2.
//  Copyright © 2016年 pengpeng. All rights reserved.
//

#import "PGWebViewController.h"
#import <WebKit/WebKit.h>


@interface PGWebViewController ()
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@property (weak, nonatomic) IBOutlet UIView *containView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *backItem;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *forwardItem;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *refreshItem;
/**<#name#>*/
@property (nonatomic, weak) WKWebView *webView;
@end

@implementation PGWebViewController



- (void)viewDidLoad {
    [super viewDidLoad];
//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    WKWebView *webView = [[WKWebView alloc] init];
//    NSLog(@"_containView--%@",NSStringFromCGRect(_containView.frame));
//    NSLog(@"View--%@",NSStringFromCGRect(_containView.frame));
    CGRect temp = [UIScreen mainScreen].bounds;
//    temp.origin.y = 64;
    webView.frame = temp;
    [_containView addSubview:webView];
    _webView = webView;
    
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    [webView loadRequest:request];
//    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
    
//    webView.canGoBack;
//    webView.canGoForward;
    
    [webView addObserver:self forKeyPath:@"canGoBack" options:NSKeyValueObservingOptionNew context:nil];
    [webView addObserver:self forKeyPath:@"canGoForward" options:NSKeyValueObservingOptionNew context:nil];
    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
     [webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"_content%@",NSStringFromCGRect(_containView.frame));
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    _backItem.enabled = _webView.canGoBack;
    _forwardItem.enabled = _webView.goForward;
    _progressView.progress = _webView.estimatedProgress;
    if (_webView.estimatedProgress>=0.5){
        int a = 2;
        int b = 5;
        NSLog(@"a,b");
    
    }
    _progressView.hidden = _webView.estimatedProgress>=1;
    self.title = _webView.title;
}


- (void)dealloc {
    [_webView removeObserver:self forKeyPath:@"canGoBack"];
    [_webView removeObserver:self forKeyPath:@"canGoForward"];
    [_webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [_webView removeObserver:self forKeyPath:@"title"];
}


- (IBAction)goaBack:(id)sender {
    [_webView goBack];
}

- (IBAction)goForward:(id)sender {
    [_webView goForward];
}

- (IBAction)refresh:(id)sender {
    [_webView reload];
}

@end
